package com.qisu.command;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author alsan
 *
 */
public class CommandFieldsList {
	private Map<String, CommandFieldDef<?>> list = new LinkedHashMap<>();

	public boolean isInitialized() {
		return list.size() > 0;
	}

	public boolean hasFieldDefine(String id) {
		return list.containsKey(id);
	}

	public void addField(String id, CommandFieldDef<?> fieldDefinitation) {
		list.put(id, fieldDefinitation);
	}

	public CommandFieldsList addField(CommandFieldDef<?> fieldDefinitation) {
		String id = (fieldDefinitation.hasConverter() ? fieldDefinitation.getDescription() : "unknown" + list.size());

		list.put(id, fieldDefinitation);

		return this;
	}

	public CommandFieldDef<?> getDef(String id) {
		if(hasFieldDefine(id)) {
			return list.get(id);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public <T> T getValue(String id) {
		CommandFieldDef<?> fieldDef = getDef(id);

		if(null != fieldDef) {
			return (T)fieldDef.getValue();
		}
		return null;
	}

	public Set<String> getFieldIdList() {
		return list.keySet();
	}
}
